﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using System.Xml.Serialization;

namespace Common
{
    public static class Serializer
    {
        public static byte[] BinarySerialize<T>(T obj)
        {
            byte[] buffer = null;
            using (MemoryStream memory = new MemoryStream())
            {
                XmlSerializer formatter = new XmlSerializer(typeof(T));
                formatter.Serialize(memory, obj);
                buffer = memory.ToArray();
            }
            return buffer;
        }

        public static T BinaryDeSerialize<T>(byte[] buffer) where T : class
        {
            T t = default(T);
            using (MemoryStream memory = new MemoryStream(buffer))
            {
                XmlSerializer formatter = new XmlSerializer(typeof(T));
                t = formatter.Deserialize(memory) as T;
            }
            return t;
        }
    }
}
